package com.crm.service.impl.customerManager;

import com.crm.common.model.Enum.StatusCodeEnum;
import com.crm.dao.mapper.customMapper.customerManagerCustomMapper.CustomerRelationCustomMapper;
import com.crm.dao.mapper.customerManager.CustomerRelationMapper;
import com.crm.model.entity.customerManager.*;
import com.crm.model.vo.customerManager.CustomerRelationVO;
import com.crm.service.customerManager.CustomerRelationDbService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * @ Author ：baijian
 * @ Date   ：2021/7/11 15:56
 */
@Service
public class CustomerRelationDbServiceImpl implements CustomerRelationDbService {

    @Autowired
    CustomerRelationMapper customerRelationMapper;

    @Autowired
    CustomerRelationCustomMapper customerRelationCustomMapper;

    /**
     * 查询客户关联
     * @param customerId 客户id
     * @return
     */
    @Override
    public List<CustomerRelation> selectCustomerRelationList(String customerId) {
        CustomerRelationExample customerRelationExample = new CustomerRelationExample();
        customerRelationExample.setOrderByClause("create_tm desc");
        CustomerRelationExample.Criteria criteria = customerRelationExample.createCriteria();
        criteria.andCustomerIdEqualTo(customerId);
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        List<CustomerRelation> list = customerRelationMapper.selectByExample(customerRelationExample);
        return list;
    }

    /**
     * 新增客户关联
     * @param customerRelation
     * @return
     */
    @Override
    public Boolean addCustomerRelation(CustomerRelation customerRelation) {
        Boolean result = false;
        int num = customerRelationMapper.insertSelective(customerRelation);
        if (num > 0) {
            result = true;
        }
        return result;
    }

    /**
     * 修改客户关联
     * @param customerRelation
     * @return
     */
    @Override
    public Boolean updateCustomerRelation(CustomerRelation customerRelation) {
        Boolean result = false;
        int num = customerRelationMapper.updateByPrimaryKeySelective(customerRelation);
        if (num > 0) {
            result = true;
        }
        return result;
    }

    /**
     * 客户关联审核列表
     * @param dateType 全部  all  本月 thisMonth 本周 thisWeek
     * @param customerName 客户名称
     * @return
     */
    @Override
    public List<CustomerRelationVO> examineCustomerRelationList(Integer dateType,String customerName) {
        return customerRelationCustomMapper.examineCustomerRelationList(dateType,customerName);
    }

    /**
     * 通过关联id查询关联信息
     * @param customerRelationId 关联id
     * @return
     */
    @Override
    public CustomerRelation selectCustomerRelation(String customerRelationId) {
        return customerRelationMapper.selectByPrimaryKey(customerRelationId);
    }

    /**
     *通过客户id和违规时间查询客户关联信息
     * @param customerId 客户id
     * @return
     */
    @Override
    public CustomerRelation selectCustomerRelationViolationTm(String customerId) {
        return customerRelationCustomMapper.selectCustomerRelationViolationTm(customerId);
    }

    /**
     * 修改客户关联 抹掉违规时间
     * @param relation 关联
     * @return
     */
    @Override
    public Boolean updateRelation(CustomerRelation relation) {
        return customerRelationCustomMapper.updateRelation(relation);
    }

    /**
     * 新增客户判重
     * @Author pym
     * @Date  2021/8/10 15:29
     * @param relationName 客户名称
     * @return {@link {java.lang.Boolean}}
     */
    @Override
    public Boolean selectCustomerRelationByName(String relationName) {
        Boolean result = false;
        CustomerRelationExample customerRelationExample = new CustomerRelationExample();
        CustomerRelationExample.Criteria criteria = customerRelationExample.createCriteria();
        criteria.andRelationNameEqualTo(relationName);
        criteria.andInspectionTypeNotEqualTo(1);
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        List<CustomerRelation> customerRelationList = customerRelationMapper.selectByExample(customerRelationExample);
        if (customerRelationList != null && customerRelationList.size() > 0) {
            result = true;
        }
        return result;
    }

    /**
     * 通过类型查询
     * @param relationType 曾用名类型 0    关联类型 1
     * @return
     */
    @Override
    public List<CustomerRelation> selectRelationType(int relationType) {
        CustomerRelationExample customerRelationExample = new CustomerRelationExample();
        customerRelationExample.setOrderByClause("create_tm desc");
        CustomerRelationExample.Criteria criteria = customerRelationExample.createCriteria();
        criteria.andRelationTypeEqualTo(relationType);
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        List<CustomerRelation> list = customerRelationMapper.selectByExample(customerRelationExample);
        return list;
    }

    /**
     * 客服首页-关联未审核数量
     * @Author renshaorong
     * @Date 2021/12/17
     * @return
     */
    @Override
    public Integer getCustomerRelationNum() {
        CustomerRelationExample example = new CustomerRelationExample();
        example.or().andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())))
                .andInspectionTypeEqualTo(2);
        return customerRelationMapper.selectByExample(example).size();
    }


}
